package com.nbjtjc.safe.api.edu;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.edu.EduCertificate;
import com.nbjtjc.safe.model.edu.EduCertificateRetrain;
import com.nbjtjc.safe.model.edu.EduPlanList;
import com.nbjtjc.safe.service.edu.EduCertificateService;

@RestController
@CrossOrigin("*")
@RequestMapping("/eduCert")
public class EduCertificateController {
	@Autowired
	private EduCertificateService eduCertificateService;


	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
	  @SysLog("添加或更新持证培训")
	    public Result add(@RequestBody EduCertificate eduCertificate){
	        Result result=new Result();
	        try {
	            result=eduCertificateService.add(eduCertificate);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	 
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除持证培训")
	    public Result delete(@RequestBody EduCertificate eduCertificate) {
	        Result result=new Result();
	        try {
	            result=eduCertificateService.delete(eduCertificate.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  
	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody EduCertificate eduCertificate) {
	        Result result = new Result();
	        try {
	            result = eduCertificateService.findDetail(eduCertificate);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  
	  @RequestMapping(value = "/show", method = {RequestMethod.POST})
	  public Result show(@RequestBody  EduCertificate eduCertificate) {
	        Result result = new Result();
	        try {	            
	            result = eduCertificateService.show(eduCertificate.getId());  
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  
	  @RequestMapping(value = "/batchRetrain", method = {RequestMethod.POST})
	  public Result batchRetrain(@RequestBody EduCertificate eduCertificate) {
	        Result result = new Result();
	        try {	            
	            result = eduCertificateService.batchRetrain(eduCertificate);  
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	  }
	  
	  
	  
	  
    @SysLog("下载持证人员表")
    @RequestMapping(value = "/eduCertificateExcelOutput",method = {RequestMethod.GET,RequestMethod.POST})
    public Result eduCertificateExcelOutput(HttpServletResponse response,@RequestBody EduCertificate eduCertificate){
        Result result=new Result();

		try {
			result = eduCertificateService.eduCertificateExcelOutput(response,eduCertificate);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return result;
    }
	 
    @SysLog("导入人员证书")
    @RequestMapping(value = "/addBatch",method = {RequestMethod.POST})
    public Result addBatch(HttpServletRequest request){
        Result result = new Result();
        try {
            // 解析器解析request的上下文未登录或者过期
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            // 先判断request中是否包含mutipart类型的数据
            if (multipartResolver.isMultipart(request)) {
                // 再将request中得数据转化成multipart类型的数据f
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                List<MultipartFile> multipartFiles = multiRequest.getFiles("file");
                Iterator iterator = multiRequest.getFileNames();
                while (iterator.hasNext()) {
                    MultipartFile file = multiRequest.getFile((String) iterator.next());
                    result = eduCertificateService.addBatch(file);
                    if (result.isSuccess()) {
                    } else {
                        result.setFalse(result.getMessage());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("导入失败" + e.getMessage());
        }
        return result;
    }
}
